package com.Logistics.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/serviceMsg/msg")
public class sendMsg {

    @PostMapping("/{phoneNumber}")
    boolean yzm(@PathVariable String phoneNumber)throws Exception{//发送验证码
        int randomCode = new Random().nextInt(999999 - 100000 + 1) + 100000;
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tC2qZG2PxT3i5PTBpNi", "uC6j0hkt29AvjUjzj19NXDqjf87PMc");
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber);
        request.setSignName("硅谷物流");
        request.setTemplateCode("SMS_471475294");
        request.setTemplateParam("{\"code\":\"" + randomCode + "\"}");
        request.setOutId("yourOutId");
        SendSmsResponse sendSmsResponse = new DefaultAcsClient(profile).getAcsResponse(request);
        Jedis jedis = new Jedis("192.168.137.148",6379);
        Integer min = 60000;
        //将权限存在redis中
        jedis.psetex("msg", min, String.valueOf(randomCode));
        jedis.close();
        return true;
    }
}
